package com.project.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.project.bean.FolloweBean;
import com.project.bean.PageBean;
import com.project.dao.FollowMapper;
import com.project.service.IFollowService;
import com.project.util.GetCurrentTime;
@Service
public class FollowServiceImpl implements IFollowService {
	@Autowired
	private FollowMapper followdao;
	
	/**
	 * 添加关注
	 * id1为自己ID，id2为对方ID
	 */
	@Override
	public int addFollow(int id1, int id2) {
		//获取当前时间
				String time = GetCurrentTime.getNow();
				//添加关注前查询对方是否关注自己
				int s=followdao.isFollowed(id2, id1);
				if(s==0){
					int i =followdao.isFollowed(id1, id2);
					//关注状态
					int state=0;
					//添加是否成功返回的int
					int result=1;
					if(i==1) {
						//查询出一条数据，对方关注自己，则将对方的关注state由1（单方面关注）修改为2（互相关注）
						state = 2;
						followdao.updateState(id1, id2,state);
						//再添加关注state为2,添加时把时间添上
						result = followdao.addFollow(id1, id2, time,state);
					}else{
						//对方未关注自己，直接添加，关注状态state为1
						result = followdao.addFollow(id1, id2, time,state);
					}
					return result;
				}else{
					return 1;
				}

	}
	/**
	 * 取消关注
	 */
	@Override
	public int delFollow(int fid,int id1,int id2) {
		int result = 0;
		//删除前判断对方是否关注自己
		int i =followdao.isFollowed(id1, id2);
		if(i==1) {
			//对方关注自己，需要将对方关注状态修改为1
			int state =1;
			followdao.updateState(id1, id2, state);
		}
		//执行删除
		result = followdao.deleteByPrimaryKey(fid);

		return result;
	}
	/**
	 * 分页查询关注列表
	 */
	@Override
	public PageBean findFollowsByPage(int id, int page, int size) {
		PageBean bean = new PageBean();
		List<FolloweBean> list = followdao.findFollows(id, (page-1)*size, size);
		int number = followdao.findFollowsNumber(id);
		int totalPage=(number%size==0)?(number/size):(number/size+1);
		bean.setList(list);
		bean.setPage(page);
		bean.setSize(size);
		bean.setTotalNumber(number);
		bean.setTotalPage(totalPage);
		return bean;
	}
	/**
	 * 分页查询粉丝列表
	 */
	@Override
	public PageBean findFansByPage(int id, int page, int size) {
		PageBean bean = new PageBean();
		List<FolloweBean> list = followdao.findFans(id, (page-1)*size, size);
		int number = followdao.findFansNumber(id);
		int totalPage=(number%size==0)?(number/size):(number/size+1);
		bean.setList(list);
		bean.setPage(page);
		bean.setSize(size);
		bean.setTotalNumber(number);
		bean.setTotalPage(totalPage);
		return bean;
	}
	/**
	 * 分页查询好友列表
	 */
	@Override
	public PageBean findFriendsByPage(int id, int page, int size) {
		PageBean bean = new PageBean();
		List<FolloweBean> list = followdao.findFriends(id, (page-1)*size, size);
		int number = followdao.findFriendsNumber(id);
		int totalPage=(number%size==0)?(number/size):(number/size+1);
		bean.setList(list);
		bean.setPage(page);
		bean.setSize(size);
		bean.setTotalNumber(number);
		bean.setTotalPage(totalPage);
		return bean;
	}
	/**
	 * 查询关注数量
	 */
	@Override
	public int findFollowsNumber(int id) {
		int number = followdao.findFollowsNumber(id);
		return number;
	}
	/**
	 * 查询粉丝数量
	 */
	@Override
	public int findFansNumber(int id) {
		int number = followdao.findFansNumber(id);
		return number;
	}
	/**
	 * 查询好友数量
	 */
	@Override
	public int findFriendsNumber(int id) {
		int number = followdao.findFriendsNumber(id);
		return number;
	}
	/**
	 * 查询关注列表
	 */
	@Override
	public List<FolloweBean> findFollowsById(int id) {
		List<FolloweBean> list = followdao.findFollowsById(id);
		return list;
	}
	/**
	 * 查询粉丝列表
	 */
	@Override
	public List<FolloweBean> findFansById(int id) {
		List<FolloweBean> list=followdao.findFansById(id); 
		return list;
	}
	@Override
	public int findFollow(Integer uid, int cUId) {
		// TODO Auto-generated method stub
		return followdao.findFollow(uid, cUId);
	}
	
	 @Override
		public int deleteUserFollow(int id1, int id2) {
			int result = 0;
			//删除前判断对方是否关注自己
			int i =followdao.isFollowed(id1, id2);
			if(i==1) {
				//对方关注自己，需要将对方关注状态修改为1
				int state =1;
				followdao.updateState(id1, id2, state);
			}
			return followdao.deleteUserFollow(id1, id2);
		}
    
}
